<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品录入
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 * @version 1.0
 */
define('GIF',1);
define('JPG',2);
define('PNG',3);

class goodsAdd {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }
    
    /**
     * 根据商品基本信息组获取到基本信息值
     * @param $arg_array 参数数组 array=(1,2,3,.....); 信息组ID
     */
    public function getBasicTpl( $arg_array ){
    	//检验参数
    	if (!isset($arg_array['params']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}
    	
    	$group_str = '';
    	$basic_array = array(
    		'useFlg' => 1
    	);
    	$basic_sort = array();
    	foreach ($arg_array['params'] as $p) {
    		$group_str .= $p . ',';
    		$basic_sort[] = $p;
    	}
    	if (!empty($group_str)) {
			$group_str = substr($group_str, 0, -1);
			$basic_array['extend'] = " AND basicGroupID IN ($group_str) ORDER BY basicGroupID ASC,sortOrder ASC";
    	} else {
    		return array('code' => 400, 'message' => '商品基本信息未找到');
    	}
    	
    	quanli_include('module/base/goods/goodsBasic.class.php');
    	$basicHandler = new goodsBasic( $this->dbca );
    	$basic_result = $basicHandler->searchBasic($basic_array);
    	if (!$basic_result) {
    		return array('code' => 400, 'message' => '商品基本信息未找到');
    	}

    	return array('code' => 200, 'data' => $basic_result, 'sort' => $basic_sort);

    }
    
    /**
     * 查询商品原材料信息
     * @param $arg_array 参数数组 
     */
    public function getMaterTpl( $arg_array ){
    	
    	//检验参数
    	if (!isset($arg_array['params']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}
    	
    	$group_str = '';
    	$mater_array = array();
    	$mater_sort = array();
    	foreach ($arg_array['params'] as $p) {
    		$group_str .= $p . ',';
    		$mater_sort[] = $p;
    	}
    	if (!empty($group_str)) {
			$group_str = substr($group_str, 0, -1);
			$mater_array['extend'] = " AND materID IN ($group_str) ORDER BY materID ASC";
    	} else {
    		return array('code' => 400, 'message' => '商品原材料信息未找到');
    	}
    	
    	quanli_include('module/base/goods/goodsMater.class.php');
    	$materHandler = new goodsMater( $this->dbca );
    	$mater_result = $materHandler->searchMater($mater_array);
    	if (!$mater_result) {
    		return array('code' => 400, 'message' => '商品原材料信息未找到');
    	}

    	return array('code' => 200, 'data' => $mater_result, 'sort' => $mater_sort);

    }
    
    /**
     * 添加or编辑商品
     *
     * @param 参数数组 $arg_array
     */
    public function addGoods( $arg_array ){
    	
    	//检验参数
    	if (!isset($arg_array['params'], $arg_array['creator']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}

    	$error_msg = '';
    	
    	$params = $arg_array['params'];	//传递的参数
    	
    	$imgArray = array();	//图片数组
    	
    	$basicArray = array();	//基本信息数组
    	
    	$materArray = array();	//原材料信息数组
    	
    	$tagArray = array();	//商品标签
    	
    	if (!isset($params['postID']) || empty($params['postID'])) {	//添加
    		
    		//城市
//    		if (!isset($params['cityID']) || empty($params['cityID'])) {
//    			$error_msg .= '请选择商品所属城市<br/>';
//    		} else {
//    			quanli_include('module/base/system/sysRegion.class.php');
//			    $sysRegion = new sysRegion( $this->dbca );
//			    $sys_array = array(
//			        'region_level' => 2,
//			        'id' => $params['cityID']
//			    );
//			    $sys_result = $sysRegion->getAreaManageList( $sys_array );
//			    if (!$sys_result) {
//			    	return array('code' => 404, 'message' => '城市信息未找到，请刷新页面后重新操作');
//			    }
//			    $sys_result = $sys_result[0];
//
//			    //城市名称
//			    $params['cityName'] = $sys_result['name'];
//
//	    		//分类排序
//	    		quanli_include('module/base/goods/quanliGoods.class.php');
//	    		$goodsHandler = new quanliGoods( $this->dbca );
//	    		$params['sortOrder'] = $goodsHandler->maxListEditSort($params['cityID']);
//    		}

    		//类目
    		if (!isset($params['categoryID']) || empty($params['categoryID'])) {
    			$error_msg .= '请选择商品类目<br/>';
    		} else {
    			quanli_include('module/base/goods/goodsCategory.class.php');
    			$categoryHandler = new goodsCategory( $this->dbca );
    			$category_array = array(
    				'bigCategoryID' => $params['categoryID']
    			);
    			$category_result = $categoryHandler->searchBigCategory($category_array);
    			if (!$category_result) {
    				return array('code' => 404, 'message' => '商品类目信息未找到，请刷新页面后重新操作');
    			} else {
    				$category_result = $category_result[0];
    				$params['categoryName'] = $category_result['name'];
    				if ($category_result['useFlg'] != 1) {
    					return array('code' => 404, 'message' => '商品类目「' . $category_result['name'] . '」已禁用，请刷新页面后重新操作');
    				}
    			}
    		}
    		
    		//商品名称
    		if (!isset($params['goodsName']) || empty($params['goodsName'])) {
    			$error_msg .= '请填写商品中文名称<br/>';
    		} else {
    			if (strlen($params['goodsName']) / 3 > 30) {
    				$error_msg .= '商品中文名称必须在15个汉字以内<br/>';
    			}
    		}
    		
//    		if (!isset($params['frenchName']) || empty($params['frenchName'])) {
//    			$error_msg .= '请填写商品法文名称<br/>';
//    		} else {
//    			if (strlen($params['frenchName']) > 31) {
//    				$error_msg .= '商品法文名称必须在30个字符以内<br/>';
//    			}
//    		}

            //商品品牌
            if (!isset($params['brandID']) || empty($params['brandID'])) {
                $error_msg .= '请选择商品品牌<br/>';
            } else {
                quanli_include('module/base/system/itemHandler.class.php');
                $itemHandler = new itemHandler( $this->dbca );
                $item_array = array(
                    'useFlg' => 1,
                    'itemID' => $params['brandID']
                );
                $brand_array = $itemHandler->searchItem( $item_array );
                if (!$brand_array) {
                    return array('code' => 404, 'message' => '商品品牌不存在或已禁用');
                }
                $brand_array = $brand_array[0];
                $params['brandName'] = $brand_array['itemName'];
            }
    		
    		//生成mainPostID、goodsID、postID
    		$params['mainPostID'] = generate_seqno('seqno_post');
    		$params['postID'] = generate_seqno('seqno_post');
    		$params['goodsID'] = generate_seqno('seqno_goods');
    		
    	} else {
    		//检查主商品是否存在
    		quanli_include('module/base/goods/quanliGoods.class.php');
    		$goodsHandler = new quanliGoods( $this->dbca );
    		$goods_array = array(
    			'postID' => $params['postID']
    		);
    		$goods_result = $goodsHandler->searchListEdit( $goods_array );
    		if (!$goods_result) {
	    		return array('code' => 404, 'message' => '商品录入失败，请刷新页面后重新操作');
    		}
    		
    		$goods_result = $goods_result[0];
			$params['mainPostID'] = $goods_result['mainPostID'];
			$params['goodsID'] = $goods_result['goodsID'];
			$params['sortOrder'] = $goods_result['sortOrder'];

    	}

    	//促销标签
//    	if (isset($params['tagID']) && !empty($params['tagID'])) {
//    		quanli_include('module/base/goods/goodsPromotion.class.php');
//	        $tagHandler = new goodsPromotion( $this->dbca );
//	        $tag_array = array(
//	        	'useFlg' => 1,
//	        	'tagID' => $params['tagID']
//	        );
//	        $promotionTag = $tagHandler->searchPromotionTag( $tag_array );
//	        if (!$promotionTag) {
//	        	return array('code' => 404, 'message' => '商品促销标签不存在或已禁用');
//	        }
//	        $promotionTag = $promotionTag[0];
//	        $params['tagName'] = $promotionTag['tagName'];
//	        $params['tagImg'] = $promotionTag['orgitalImage'];
//    	}
    	
    	//商品品牌
    	if (isset($params['brandID']) && !empty($params['brandID'])) {
    		quanli_include('module/base/system/itemHandler.class.php');
	        $itemHandler = new itemHandler( $this->dbca );
	        $item_array = array(
	        	'useFlg' => 1,
	        	'itemID' => $params['brandID']
	        );
	        $brand_array = $itemHandler->searchItem( $item_array );
	        if (!$brand_array) {
	        	return array('code' => 404, 'message' => '商品品牌不存在或已禁用');
	        }
	        $brand_array = $brand_array[0];
	        $params['brandName'] = $brand_array['itemName'];
    	}
    	
    	/*
    	//商品图片
    	if (isset($params['cover']) && in_array($params['cover'], array(1, 2, 3, 4, 5))) {
    		for ($i=1; $i<=5; $i++) {
	    		$imgname = 'imgname_' . $i;
	    		$isort = 'iSort_' . $i;
	    		if (empty($params[$imgname]) && $i == $params['cover']) {
	    			$error_msg .= '图片上传错误，请刷新页面后重新操作<br/>';
	    			break;
	    		}

	    		if (!empty($params[$imgname])) {
	    			if (!quanli_is_int($params[$isort])) {
	    				$error_msg .= '请填写正确的商品图片排序<br/>';
	    				break;
	    			}

	    			//检查商品图片是否上传成功
	    			$upload_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/kindeditor/' . $params[$imgname];
	    			$old_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/goods/' . $params[$imgname];

	    			if (!@file_exists($upload_path) && !@file_exists($old_path)) {
	    				return array('code' => 404, 'message' => '图片未上传成功，请刷新页面后重新上传');
	    			}

	    			if ($i == $params['cover']) {
	    				$imgArray[] = array(
		    				'path' => $params[$imgname],
		    				'sortOrder' => $params[$isort],
		    				'mainFlg' => 1
		    			);
	    			} else {
	    				$imgArray[] = array(
		    				'path' => $params[$imgname],
		    				'sortOrder' => $params[$isort],
		    				'mainFlg' => 0
		    			);
	    			}

	    		}
	    	}

	    	if (empty($imgArray)) {
	    		$error_msg .= '请上传商品图片并设置封面<br/>';
	    	}
    	} else {
    		$error_msg .= '请上传商品图片并设置封面<br/>';
    	}

    	//图片title
    	if (isset($params['goodsPicTitle']) && !empty($params['goodsPicTitle'])) {
    		if (strlen($params['goodsPicTitle']) / 3 > 2000) {
    			$error_msg .= '图片title必须在1000个字符以内<br/>';
    		}
    	} else {
    		$params['goodsPicTitle'] = '';
    	}

    	//图片alt
    	if (isset($params['goodsPicAlt']) && !empty($params['goodsPicAlt'])) {
    		if (strlen($params['goodsPicAlt']) / 3 > 2000) {
    			$error_msg .= '图片alt必须在1000个字符以内<br/>';
    		}
    	} else {
    		$params['goodsPicAlt'] = '';
    	}

    	//商品介绍文案
    	if (!isset($params['chineseBrief']) || empty($params['chineseBrief'])) {
    		$error_msg .= '请填写商品中文介绍文案<br/>';
    	} else {
    		if (strlen($params['chineseBrief']) / 3 > 150) {
    			$error_msg .= '商品中文介绍文案必须在150个汉字以内<br/>';
    		}
    	}

    	if (!isset($params['frenchBrief']) || empty($params['frenchBrief'])) {
    		$error_msg .= '请填写商品英文介绍文案<br/>';
    	} else {
    		if (strlen($params['frenchBrief']) > 300) {
    			$error_msg .= '商品法文介绍文案必须在300个汉字以内<br/>';
    		}
    	}

    	商品基本信息
    	if (isset($params['selectBasicGroupID']) && !empty($params['selectBasicGroupID'])) {
    		foreach ($params['selectBasicGroupID'] as $basic) {
    			//检查基本信息组是否存在和启用
    			quanli_include('module/base/goods/goodsBasic.class.php');
    			$basicHandler = new goodsBasic( $this->dbca );
    			$basicGroup_array = array(
    				'basicGroupID' => $basic
    			);
    			$basicGroup_result = $basicHandler->searchBasicGroup($basicGroup_array);
    			if (!$basicGroup_result) {
    				return array('code' => 404, 'message' => '商品基本信息不存在，请刷新页面后重新操作');
    			}

    			$basicGroup_result = $basicGroup_result[0];
				if ($basicGroup_result['useFlg'] != 1) {
					return array('code' => 404, 'message' => '商品基本信息「' . $basicGroup_result['chinese'] . '」已禁用，请刷新页面后重新操作');
				}

    			$basicId = 'basicID_' . $basic;
    			$bsort = 'bsort_' . $basic;
    			if (!isset($params[$basicId]) || !quanli_is_int($params[$basicId])) {
    				$error_msg .= '请选择商品基本信息<br/>';
    				break;
    			} else {
    				$basic_array = array(
	    				'basicID' => $params[$basicId]
	    			);
    				$basic_result = $basicHandler->searchBasic($basic_array);
    				if (!$basic_result) {
    					return array('code' => 404, 'message' => '商品基本信息不存在，请刷新页面后重新操作');
    				}

    				$basic_result = $basic_result[0];
					if ($basic_result['useFlg'] != 1) {
						return array('code' => 404, 'message' => '商品基本信息维度「' . $basic_result['basicName'] . '」已禁用，请刷新页面后重新操作');
					}
    			}

    			if (!isset($params[$bsort]) || !quanli_is_int($params[$bsort])) {
    				$error_msg .= '请填写正确的商品基本信息排序<br/>';
    				break;
    			}

    			$basicArray[] = array(
    				'basicGroupID' => $basic,
    				'chinese' => isset($basicGroup_result['chinese']) ? $basicGroup_result['chinese'] : '',
    				'french' => isset($basicGroup_result['french']) ? $basicGroup_result['french'] : '',
    				'basicID' => isset($params[$basicId]) ? $params[$basicId] : 0,
    				'basicName' => isset($basic_result['basicName']) ? $basic_result['basicName'] : '',
    				'basicVal' => isset($basic_result['basicVal']) ? $basic_result['basicVal'] : '',
    				'sortOrder' => isset($params[$bsort]) ? $params[$bsort] : 1,
    			);
    		}
    	}

    	//商品原材料信息
    	if (isset($params['selectMater']) && !empty($params['selectMater'])) {
    		foreach ($params['selectMater'] as $mater) {
    			//检查商品原材料信息是否存在或者禁用
    			quanli_include('module/base/goods/goodsMater.class.php');
    			$materHandler = new goodsMater( $this->dbca );
    			$mater_array = array(
    				'materID' => $mater
    			);
    			$mater_result = $materHandler->searchMater($mater_array);
    			if (!$mater_result) {
    				return array('code' => 404, 'message' => '商品原材料信息不存在，请刷新页面后重新操作');
    			}
    			$mater_result = $mater_result[0];
				if ($mater_result['useFlg'] != 1) {
					return array('code' => 404, 'message' => '商品原材料信息「' . $mater_result['materName'] . '」已禁用，请刷新页面后重新操作');
				}

    			$msort = 'msort_' .  $mater;
    			if (!isset($params[$msort]) || empty($params[$msort])) {
    				$error_msg .= '请填写商品原材料信息排序<br/>';
    				break;
    			}

    			$materArray[] = array(
    				'materID' => $mater,
    				'materName' => isset($mater_result['materName']) ? $mater_result['materName'] : '',
    				'sortOrder' => isset($params[$msort]) ? $params[$msort] : 1,
    				'image' => isset($mater_result['originalImage']) ? $mater_result['originalImage'] : '',
    			);
    		}
    	}

    	//标签
    	if (isset($params['tag']) && !empty($params['tag'])) {
    		if (strlen($params['tag']) / 3 > 2000) {
    			$error_msg .= '商品TAG必须在1000个字符以内<br/>';
    		}
    	} else {
    		$params['tag'] = '';
    	}

    	//商品标签
    	if (isset($params['tag_box']) && !empty($params['tag_box'])) {
    		quanli_include('module/base/goods/goodsTag.class.php');
    		$tagHandler = new goodsTag( $this->dbca );
    		foreach ($params['tag_box'] as $tag) {
	    		$tag_array = array(
		         	'useFlg' => 1,
		         	'tagID' => $tag
	    		);
	    		$tag_array = $tagHandler->goodsTagList( $tag_array );
	    		if (!$tag_array) {
	    			return array('code' => 400, 'message' => '商品标签未找到或已禁用');
	    		}
	    		$tag_array = $tag_array[0];
	    		$tagArray[] = array(
	    			'tagGroupID' => $tag_array['tagGroupID'],
	    			'tagGroupName' => $tag_array['tagGroupName'],
	    			'tagID' => $tag_array['tagID'],
	    			'tagName' => $tag_array['tagName']
	    		);
    		}
    	}
        */

        if (!empty($error_msg)) {
    		return array('code' => 400, 'message' => $error_msg);
    	}

    	//二级待发布表
    	$save_list_edit = new entity('goods_list');
    	$save_list_edit->postID = $params['postID'];
    	if (!$this->dbca->fetch($save_list_edit)) {
    		$save_list_edit->creator = $arg_array['creator'];
    		$save_list_edit->cityID = 0; //$params['cityID'];
    		$save_list_edit->cityName = '';//$params['cityName'];
    		$save_list_edit->bigCategoryID = $params['categoryID'];
    		$save_list_edit->bigCategoryName = $params['categoryName'];
    		$save_list_edit->midCategoryID = 0;
    		$save_list_edit->midCategoryName = '';
    		$save_list_edit->smallCategoryID = 0;
    		$save_list_edit->smallCategoryName = '';
    		$save_list_edit->goodsName = $params['goodsName'];
    		$save_list_edit->frenchName = '';//$params['frenchName'];
    		$save_list_edit->mainPostID = $params['mainPostID'];
    		$save_list_edit->goodsID = $params['goodsID'];
    		$save_list_edit->product_code = $params['postID'];
    		$save_list_edit->useFlg = 0;
	    	$save_list_edit->forsale = 1;
	    	$save_list_edit->foretaste = 0;
	    	$save_list_edit->spec = '';
	    	$save_list_edit->marketPrice = 0;
	    	$save_list_edit->salePrice = 0;
	    	$save_list_edit->discountFlg = 1;
	    	$save_list_edit->description = '';
	    	$save_list_edit->uid = 0;
	    	$save_list_edit->goodsMainPic = '';
    	} else {
    		$save_list_edit->editor = $arg_array['creator'];
    	}
//    	$save_list_edit->tag = $params['tag'];
//    	$save_list_edit->chineseBrief = $params['chineseBrief'];
//    	$save_list_edit->frenchBrief = $params['frenchBrief'];
//    	$save_list_edit->isPublish = 0;
//    	$save_list_edit->goodsPicTitle = $params['goodsPicTitle'];
//    	$save_list_edit->goodsPicAlt = $params['goodsPicAlt'];
//    	$save_list_edit->sortOrder = $params['sortOrder'];
//    	$save_list_edit->sortOrder = $params['sortOrder'];
//    	$save_list_edit->sortOrder = $params['sortOrder'];
//    	$save_list_edit->tagID = isset($params['tagID']) ? $params['tagID'] : '';
//    	$save_list_edit->tagName = isset($params['tagName']) ? $params['tagName'] : '';
//    	$save_list_edit->tagImg = isset($params['tagImg']) ? $params['tagImg'] : '';
    	$save_list_edit->brandID = isset($params['brandID']) ? $params['brandID'] : '';
    	$save_list_edit->brandName = isset($params['brandName']) ? $params['brandName'] : '';
    	
    	$this->dbca->beginTransaction();
    	try {
    		/*
	    	//商品图片
	    	$this->dbca->stmt_query('DELETE FROM goods_picture WHERE postID = ?', 'i', $params['postID']);
	    	
	    	quanli_include('module/base/goods/commonGoods.class.php');
	    	$commHandler = new commonGoods( $this->dbca );
	    	if (!empty($imgArray)) {
	    		foreach ($imgArray as $img) {
	    			
	    			$pathExp = date('Ymd');
	    			$extend = strtolower(substr(strrchr($img['path'] , '.'), 1));
	    			
	    			//上传后的临时存放路径
	    			$tmpPath = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/kindeditor/' . $img['path'];
	    			if (!@file_exists($tmpPath)) {
	    				$tmpPath = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/goods/' . $img['path'];
	    			}
	    			
	    			//应该移动到的目录
	    			$saveDir = G_APPLICATION_BASEPATH.'quanliadmin/docroot/images/goods/' . $pathExp . '/' . $params['goodsID'] . '/';
	    			if (!@file_exists($saveDir)) {
	    				mkdir($saveDir, 0777, true);
	    			}
	    			
	    			//展示大图(960*960)
	    			$displayImg = 'big_' . $params['goodsID'] . '_'. $img['sortOrder'] . '.' . $extend;
	    			$saveImg = 'list_' . $params['goodsID'] . '_'. $img['sortOrder'] . '.' . $extend;
	    			
	    			//应该保存的文件
	    			if (!@file_exists($saveDir . $displayImg) || $tmpPath != $saveDir . $displayImg) {
	  
	    				if (@file_exists($saveDir . $displayImg)) {
		    				@unlink($saveDir . $displayImg);
		    			}
		    			
	    				copy($tmpPath, $saveDir . $displayImg);
	    				
	    				//产品列表缩略图 260*260
	    				$listImg = 'list_' . $params['goodsID'] . '_'. $img['sortOrder'] . '.' . $extend;
	    				if (@file_exists($saveDir . $listImg)) {
		    				@unlink($saveDir . $listImg);
		    			}
		    			$size_array = array(
		    				'width' => 216,
		    				'height' => 216
		    			);
		    			if (!$commHandler->ResizeImage($saveDir . $displayImg, $saveDir . $listImg, $size_array)) {
		    				return array('code' => 404, 'message' => '图片上传失败，请重新操作');
		    			}
		    			
		    			//详情页中图 480*480
		    			$middleImg = 'middle_' . $params['goodsID'] . '_'. $img['sortOrder'] . '.' . $extend;
		    			if (@file_exists($saveDir . $middleImg)) {
		    				@unlink($saveDir . $middleImg);
		    			}
		    			$size_array = array(
		    				'width' => 480,
		    				'height' => 480
		    			);
		    			if (!$commHandler->ResizeImage($saveDir . $displayImg, $saveDir . $middleImg, $size_array)) {
		    				return array('code' => 404, 'message' => '图片上传失败，请重新操作');
		    			}
	    				
	    				//详情页小图 100*100
		    			$smallImg = 'small_' . $params['goodsID'] . '_'. $img['sortOrder'] . '.' . $extend;
		    			if (@file_exists($saveDir . $smallImg)) {
		    				@unlink($saveDir . $smallImg);
		    			}
		    			$size_array = array(
		    				'width' => 100,
		    				'height' => 100
		    			);
		    			if (!$commHandler->ResizeImage($saveDir . $displayImg, $saveDir . $smallImg, $size_array)) {
		    				return array('code' => 404, 'message' => '图片上传失败，请重新操作');
		    			}
		    			
		    			if ($img['mainFlg'] == 1) {	//主图片
		    				//保存主图片
			    			$save_list_edit->goodsMainPic = $pathExp . '/' . $params['goodsID'] . '/' . $listImg;
		    			}
	    			}

	    			$save_goods_picture = new entity('goods_picture');
	    			$save_goods_picture->creator = $arg_array['creator'];
	    			$save_goods_picture->postID = $params['postID'];
	    			$save_goods_picture->pictureLink = $pathExp . '/' . $params['goodsID'] . '/' . $saveImg;
	    			$save_goods_picture->sortOrder = $img['sortOrder'];
	    			$save_goods_picture->mainFlg = $img['mainFlg'];
	    			$this->dbca->save($save_goods_picture);
	    		}
	    	}

	    	//商品基本信息
	    	$this->dbca->stmt_query('DELETE FROM goods_basic_relation WHERE postID = ?', 'i', $params['postID']);
	    	if (!empty($basicArray)) {
	    		foreach ($basicArray as $basic) {
		    		$save_goods_basic = new entity('goods_basic_relation');
		    		$save_goods_basic->creator = $arg_array['creator'];
		    		$save_goods_basic->postID = $params['postID'];
		    		$save_goods_basic->basicGroupID = $basic['basicGroupID'];
		    		$save_goods_basic->chinese = $basic['chinese'];
		    		$save_goods_basic->french = $basic['french'];
		    		$save_goods_basic->basicID = $basic['basicID'];
		    		$save_goods_basic->basicName = $basic['basicName'];
		    		$save_goods_basic->basicVal = $basic['basicVal'];
		    		$save_goods_basic->sortOrder = $basic['sortOrder'];
		    		$this->dbca->save($save_goods_basic);
	    		}
	    	}
	    	
	    	//商品原材料
	    	$this->dbca->stmt_query('DELETE FROM goods_mater_relation WHERE postID = ?', 'i', $params['postID']);
	    	if (!empty($materArray)) {
	    		foreach ($materArray as $mater) {
		    		$save_goods_mater = new entity('goods_mater_relation');
		    		$save_goods_mater->creator = $arg_array['creator'];
		    		$save_goods_mater->postID = $params['postID'];
		    		$save_goods_mater->materID = $mater['materID'];
		    		$save_goods_mater->materName = $mater['materName'];
		    		$save_goods_mater->sortOrder = $mater['sortOrder'];
		    		$save_goods_mater->image = $mater['image'];
		    		$this->dbca->save($save_goods_mater);
	    		}
	    	}
	    	
	    	//商品标签
	    	$this->dbca->stmt_query('DELETE FROM goods_tag_relation WHERE postID = ?', 'i', $params['postID']);
	    	if (isset($tagArray) && !empty($tagArray)) {
	    		foreach ($tagArray as $tag) {
	    			$save_tag_relation = new entity('goods_tag_relation');
	    			$save_tag_relation->creator = $arg_array['creator'];
	    			$save_tag_relation->postID = $params['postID'];
	    			$save_tag_relation->tagGroupID = $tag['tagGroupID'];
	    			$save_tag_relation->tagGroupName = $tag['tagGroupName'];
	    			$save_tag_relation->tagID = $tag['tagID'];
	    			$save_tag_relation->tagName = $tag['tagName'];
	    			$this->dbca->save($save_tag_relation);
	    		}
	    	}
            */

            //二级待发布商品
	    	$this->dbca->save($save_list_edit);
	    	
	    	//查询所以三级商品并更新主图片信息、品牌、图片title、图片alt、商品介绍文案、商品TAG
//	    	$update_sql = 'UPDATE goods_sale_edit SET goodsMainPic = ?, brandID = ?, brandName = ?,
//	    	               goodsPicTitle = ?, goodsPicAlt = ?, chineseBrief = ?, frenchBrief = ?,
//	    	               tag = ? WHERE twoPostID = ?';
//	    	$this->dbca->stmt_query($update_sql, 'sissssssi', $save_list_edit->goodsMainPic, $save_list_edit->brandID
//	    	                        , $save_list_edit->brandName, $save_list_edit->goodsPicTitle,
//	    	                        $save_list_edit->goodsPicAlt, $save_list_edit->chineseBrief,
//	    	                        $save_list_edit->frenchBrief, $save_list_edit->tag, $params['postID']);
    	
	    	$this->dbca->commit();
    		return array('code' => 200, 'message' => '商品录入成功');
    	} catch (Exception $e) {
    		error_log($e->getMessage());
    		$this->dbca->rollBack();
    		return array('code' => 400, 'message' => '商品录入失败');
    	}
    	
    }
    
    /**
     * 商品sku添加or编辑
     *
     * @param unknown_type $arg_array
     */
    public function addGoodsSku( $arg_array ){
    	
    	//基本参数检验
    	if (!isset($arg_array['params'], $arg_array['creator']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}

    	//传递的参数
    	$params = $arg_array['params'];	
    	
    	if (!isset($params['postID'], $params['tPostID'], $params['hSpecGroupID'], $params['hSpecID']) ||
    	    !is_numeric($params['postID']) || !is_numeric($params['hSpecGroupID']) || !is_numeric($params['hSpecID'])) {
    	    
    	    return array('code' => 400, 'message' => '参数错误');		
    	}
    	
    	//错误信息字符串
    	$error_msg = '';
    	
    	$syncStr = '';
    	
    	$syncIdStr = '';
    	
        $birthStr = '';
        
        $birthIdStr = '';
    	
    	$fitting_array = array();
    	
    	$dTime_array = array();
    	
//    	if (!isset($params['setMainGoods']) || !in_array($params['setMainGoods'], array(0, 1))) {
//    		$error_msg .= '请设置主商品<br/>';
//    	}

    	if (!isset($params['product_code']) || empty($params['product_code'])) {
    		$error_msg .= '请填写商品SKU<br/>';
    	}
    	
    	if (!isset($params['salePrice']) || is_money($params['salePrice'], 2) == false) {
    		$error_msg .= '请填写正确的商品售价<br/>';
    	}

//    	if (!isset($params['aheadTime']) || $params['aheadTime'] == 0) {
//    		$error_msg .= '请选择提前预定时间<br/>';
//    	}

//    	if (!isset($params['useFlg']) || !in_array($params['useFlg'], array(1, 2, 3))) {
//    		$error_msg .= '请选择网站状态<br/>';
//    	} else {
//    		if ($params['useFlg'] == 2) {
//    			$params['useFlg'] = 0;
//    		} else if ($params['useFlg'] == 3) {
//    			$params['useFlg'] = 2;
//    		}
//    	}
    	
    	if (!isset($params['forsale']) || !in_array($params['forsale'], array(1, 2))) {
    		$error_msg .= '请选择销售状态<br/>';
    	} else {
    		if ($params['forsale'] == 2) {
    			$params['forsale'] = 0;
    		}
    	}
    	
//    	if (!isset($params['foretaste']) || !in_array($params['foretaste'], array(1, 2))) {
//    		$error_msg .= '请选择试吃状态<br/>';
//    	} else {
//    		if ($params['foretaste'] == 2) {
//    			$params['foretaste'] = 0;
//    		}
//    	}
    	
//    	if (!isset($params['startDeliver']) || empty($params['startDeliver'])) {
//    		$error_msg .= '请填写起始配送时间<br/>';
//    	}
//
//    	if (!isset($params['endDeliver']) || empty($params['endDeliver'])) {
//    		$error_msg .= '请填写结束配送时间<br/>';
//    	}
//
//    	if (!isset($params['interval']) || $params['interval'] < 0) {
//    		$error_msg .= '请选择配送间隔<br/>';
//    	} else {
//    	    if ($params['interval'] == 0) {
//    			if (!isset($params['sameCount']) || !is_numeric($params['sameCount'])) {
//    				$error_msg .= '请维护“当天下单/配送时间控制”数据<br/>';
//    			}
//    		}
//    	}
//
//    	if (!isset($params['cutTime']) || empty($params['cutTime'])) {
//    	    $error_msg .= '请填写截单时间<br/>';
//    	}

    	if (!empty($error_msg)) {
    		return array('code' => 400, 'message' => $error_msg);
    	}
    	
    	// +----------------------------------------------------------------------
		// | 检查数据库
		// +----------------------------------------------------------------------
		
		//提前预定时间
    	quanli_include('module/base/system/itemHandler.class.php');
    	$itemHandler = new itemHandler( $this->dbca );
//    	$item_array = array(
//        	'useFlg' => 1,
//        	'itemID' => $params['aheadTime']
//        );
//        $aheadTime_array = $itemHandler->searchItem( $item_array );
//        if (!$aheadTime_array) {
//        	return array('code' => 404, 'message' => '提前预定时间「' . $params['aheadTimeText'] . '」不存在或已禁用');
//        }

//        if (isset($params['edible']) && $params['edible'] != 0) {
//	        //食用
//	        $item_array = array(
//	        	'useFlg' => 1,
//	        	'itemID' => $params['edible']
//	        );
//	        $edible_array = $itemHandler->searchItem( $item_array );
//	        if (!$edible_array) {
//	        	return array('code' => 404, 'message' => '食用「' . $params['edibleText'] . '」不存在或已禁用');
//	        }
//        } else {
//        	$params['edibleText'] = '';
//        }

//        if (isset($params['size']) && $params['size'] != 0) {
//        	//尺寸
//        	$item_array = array(
//	        	'useFlg' => 1,
//	        	'itemID' => $params['size']
//	        );
//	        $size_array = $itemHandler->searchItem( $item_array );
//	        if (!$size_array) {
//	        	return array('code' => 404, 'message' => '尺寸「' . $params['sizeText'] . '」不存在或已禁用');
//	        }
//        } else {
//        	$params['sizeText'] = '';
//        }

        //切分
        if (isset($params['syncopate']) && !empty($params['syncopate'])) {
        	foreach ($params['syncopate'] as $sync) {
        		$sync_array = array(
	        		'useFlg' => 1,
		        	'itemID' => $sync[0]
        		);
        		$sync_result = $itemHandler->searchItem( $sync_array );
        		if (!$sync_result) {
        			return array('code' => 404, 'message' => '切分「' . $sync[1] . '」不存在或已禁用');
        		}
        		$syncIdStr .= $sync[0] . '|';
        		$syncStr .= $sync[1] . '|';
        	}
        	if (!empty($syncStr)) {
        		$syncIdStr = substr($syncIdStr, 0, -1);
        		$syncStr = substr($syncStr, 0, -1);
        	}
        }
       
        //生日牌
        if (isset($params['birthcard']) && !empty($params['birthcard'])) {
        	foreach ($params['birthcard'] as $birth) {
        		$birth_array = array(
	        		'useFlg' => 1,
		        	'itemID' => $birth[0]
        		);
        		$birth_result = $itemHandler->searchItem( $birth_array );
        		if (!$birth_result) {
        			return array('code' => 404, 'message' => '生日牌「' . $birth[1] . '」不存在或已禁用');
        		}
        		$birthIdStr .= $birth[0] . '|';
        		$birthStr .= $birth[1] . '|';
        	}
        	if (!empty($birthStr)) {
        		$birthIdStr = substr($birthIdStr, 0, -1);
        		$birthStr = substr($birthStr, 0, -1);
        	}
        }
        
        //配件
//    	quanli_include('module/base/goods/goodsFittings.class.php');
//    	$fitHandler = new goodsFittings( $this->dbca );
//        if (isset($params['selectFit']) && !empty($params['selectFit'])) {
//        	foreach ($params['selectFit'] as $fit) {
//        		$fit_array = array(
//        			'useFlg' => 1,
//		        	'fitID' => $fit
//        		);
//
//        		$fit_result = $fitHandler->goodsFittings( $fit_array );
//        		if (!$fit_result) {
//        			return array('code' => 404, 'message' => '配件信息未找到或已禁用');
//        		}
//        		$fit_result = $fit_result[0];
//
//        		$fitCount = isset($params['fitCount_' . $fit]) ? $params['fitCount_' . $fit] : 0;
//        		$fitSale = isset($params['sale_radio_' . $fit]) ? $params['sale_radio_' . $fit] : 0;
//        		if (!quanli_is_int($fitCount)) {
//        			return array('code' => 404, 'message' => '请填写正确的配件数量');
//        		}
//
//        		if (!in_array($fitSale, array(0, 1))) {
//        			return array('code' => 404, 'message' => '请选择配件「是否可销售」');
//        		}
//
//        		if ($fitSale == 1) {	//可销售
//        			$fitPrice = isset($params['fitPrice_' . $fit]) ? $params['fitPrice_' . $fit] : 0;
//        			if (!is_money($fitPrice, 2)) {
//        				return array('code' => 404, 'message' => '请填写正确的配件售价');
//        			}
//        		} else {
//        			$fitPrice = 0;
//        		}
//
//        		$fitting_array[] = array(
//        			'fitID' => $fit,
//        			'fitName' => $fit_result['fitName'],
//        			'fitUnit' => $fit_result['fitUnit'],
//        			'sortOrder' => $fit_result['sortOrder'],
//        			'useFlg' => $fit_result['useFlg'],
//        			'fitCount' => $fitCount,
//        			'fitSale' => $fitSale,
//        			'fitPrice' => $fitPrice
//        		);
//
//        	}
//        }

        //配送时间控制
//        if (isset($params['interval']) && $params['interval'] == 0) {
//
//        	for ($i=1; $i <= $params['sameCount']; $i++) {
//        		$sameOsTime = isset($params['sameOsTime_' . $i]) ? $params['sameOsTime_' . $i] : '';
//        		$sameOeTime = isset($params['sameOeTime_' . $i]) ? $params['sameOeTime_' . $i] : '';
//        		$sameDsTime = isset($params['sameDsTime_' . $i]) ? $params['sameDsTime_' . $i] : '';
//        		$sameDeTime = isset($params['sameDeTime_' . $i]) ? $params['sameDeTime_' . $i] : '';
//
//        		if (empty($sameOsTime) && empty($sameOeTime) && empty($sameDsTime) && empty($sameDeTime)) {
//        			continue;
//        		}
//
//        		if (empty($sameOsTime) || empty($sameOeTime) || empty($sameDsTime) || empty($sameDeTime)) {
//        			return array('code' => 404, 'message' => '请填写完整的"当天下单/配送时间控制"数据');
//        		}
//
//        		$dTime_array[] = array($sameOsTime, $sameOeTime, $sameDsTime, $sameDeTime);
//        	}
//        	if (empty($dTime_array)) {
//        		return array('code' => 404, 'message' => '请填写完整的"当天下单/配送时间控制"数据');
//        	}
//        }

        //规格组
        quanli_include('module/base/goods/goodsSpec.class.php');
        $specHandler = new goodsSpec( $this->dbca );
        $specGroup_array = array(
        	'specGroupID' => $params['hSpecGroupID'],
        	'useFlg' => 1
        );
        $specGroup_result = $specHandler->goodsSpecGroupList( $specGroup_array );
        if (!$specGroup_result) {
        	return array('code' => 404, 'message' => '规格信息未找到或已禁用');
        }
        $specGroup_result = $specGroup_result[0];
        
        //规格
        $spec_array = array(
        	'specID' => $params['hSpecID'],
        	'useFlg' => 1
        );
        $spec_result = $specHandler->searchGoodsSpec( $spec_array );
        if (!$spec_result) {
        	return array('code' => 404, 'message' => '规格信息未找到或已禁用');
        }
        $spec_result = $spec_result[0];

        //二级商品
    	$save_listgoods = new entity('goods_list');
    	$save_listgoods->postID = $params['postID'];
    	if (!$this->dbca->fetch($save_listgoods)) {
    		return array('code' => 400, 'message' => '商品信息未找到');
    	}
		$save_listgoods->editor = $arg_array['creator'];
		$save_listgoods->isPublish = 0;
		
    	//三级商品
    	$save_salegoods = new entity('goods_sale');
    	if (!empty($params['tPostID'])) {
    		$save_salegoods->postID = $params['tPostID'];
    		if (!$this->dbca->fetch($save_salegoods)) {
    			return array('code' => 400, 'message' => '商品信息未找到');
    		}
    		//检查三级product_code是否重复
    		$check_product_code = new entity('goods_sale');
    		$check_product_code->product_code = $params['product_code'];
    		$check_product_code->cityID = $save_listgoods->cityID;
    		if ($this->dbca->fetch($check_product_code)) {
    			if ($check_product_code->postID != $save_salegoods->postID) {
    				return array('code' => 400, 'message' => 'SKU「' . $params['product_code'] . '」已存在');
    			}
    		}
    		$save_salegoods->editor = $arg_array['creator'];
    	} else {
    		
    		//检查三级product_code是否重复
    		$check_product_code = new entity('goods_sale');
    		$check_product_code->product_code = $params['product_code'];
    		$check_product_code->cityID = $save_listgoods->cityID;
    		if ($this->dbca->fetch($check_product_code)) {
    			return array('code' => 400, 'message' => 'SKU「' . $params['product_code'] . '」已存在');
    		}
    		
    		//检查规格是否已存在?
    		//生成三级postID、goodsID
    		$params['tPostID'] = generate_seqno('seqno_post');
    		$params['tGoodsID'] = generate_seqno('seqno_goods');

    		$save_salegoods->creator = $arg_array['creator'];
    		$save_salegoods->mainPostID = $save_listgoods->mainPostID;
    		$save_salegoods->postID = $params['tPostID'];
    		$save_salegoods->goodsID = $params['tGoodsID'];
    		$save_salegoods->twoPostID = $save_listgoods->postID;
    		$save_salegoods->goodsName = $save_listgoods->goodsName;
    		$save_salegoods->frenchName = $save_listgoods->frenchName;
    		$save_salegoods->bigCategoryID = $save_listgoods->bigCategoryID;
    		$save_salegoods->bigCategoryName = $save_listgoods->bigCategoryName;
    		$save_salegoods->midCategoryID = $save_listgoods->midCategoryID;
    		$save_salegoods->midCategoryName = $save_listgoods->midCategoryName;
    		$save_salegoods->smallCategoryID = $save_listgoods->smallCategoryID;
    		$save_salegoods->smallCategoryName = $save_listgoods->smallCategoryName;
    		$save_salegoods->smallCategoryName = $save_listgoods->smallCategoryName;
    		$save_salegoods->salePoints = ceil($params['salePrice']);
    		$save_salegoods->spec = $spec_result['specName'];
    		$save_salegoods->tag = $save_listgoods->tag;
    		$save_salegoods->chineseBrief = $save_listgoods->chineseBrief;
    		$save_salegoods->frenchBrief = $save_listgoods->frenchBrief;
    		$save_salegoods->goodsMainPic = $save_listgoods->goodsMainPic;
    		$save_salegoods->cityID = $save_listgoods->cityID;
    		$save_salegoods->cityName = $save_listgoods->cityName;
    		$save_salegoods->goodsPicTitle = $save_listgoods->goodsPicTitle;
    		$save_salegoods->goodsPicAlt = $save_listgoods->goodsPicAlt;
    		$save_salegoods->brandID = $save_listgoods->brandID;
    		$save_salegoods->brandName = $save_listgoods->brandName;
    		
    		//商品规格映射
    		$save_spec_relation = new entity('goods_spec_relation');
    		$save_spec_relation->creator = $arg_array['creator'];
    		$save_spec_relation->postID = $params['tPostID'];
    		$save_spec_relation->specGroupID = $params['hSpecGroupID'];
    		$save_spec_relation->specGroupName = $specGroup_result['specGroupName'];
    		$save_spec_relation->specID = $params['hSpecID'];
    		$save_spec_relation->specName = $spec_result['specName'];
    		$save_spec_relation->twoPostID = $save_listgoods->postID;
    	}
    	
    	//公共部分
    	$save_salegoods->product_code = $params['product_code'];
    	$save_salegoods->marketPrice = $params['salePrice'];
    	//$save_salegoods->useFlg = $params['useFlg'];
    	$save_salegoods->forsale = $params['forsale'];
    	//$save_salegoods->foretaste = $params['foretaste'];
    	//$save_salegoods->description = $params['description'];
    	$save_salegoods->salePrice = $params['salePrice'];
    	//$save_salegoods->aheadTimeID = $params['aheadTime'];
    	//$save_salegoods->aheadTime = $params['aheadTimeText'];
    	//$save_salegoods->edibleID = $params['edible'];
    	//$save_salegoods->edible = $params['edibleText'];
    	//$save_salegoods->sizeID = $params['size'];
    	//$save_salegoods->size = $params['sizeText'];
    	$save_salegoods->syncID = $syncIdStr;
    	$save_salegoods->syncopate = $syncStr;
    	$save_salegoods->birthID = $birthIdStr;
    	$save_salegoods->birthcard = $birthStr;
    	//$save_salegoods->startDeliver = $params['startDeliver'];
    	//$save_salegoods->endDeliver = $params['endDeliver'];
    	//$save_salegoods->deliverSpace = $params['interval'];
    	//$save_salegoods->cutTime = $params['cutTime'];
    	
    	//检查是否为主商品
//		if (isset($params['setMainGoods']) && $params['setMainGoods'] == 1) {
//			$save_listgoods->marketPrice = $params['salePrice'];
//			$save_listgoods->salePrice = $params['salePrice'];
//			$save_listgoods->uid = $params['tPostID'];
//		}

        //更新二级商品useFlg      add by wanglulu 20141203   start
        quanli_include('module/base/goods/quanliGoods.class.php');

        $ebsigHandler = new quanliGoods( $this->dbca );

        $sale_array = array(
            'twoPostID' => $params['postID']
        );

        $sale_goods = $ebsigHandler->searchSaleEdit( $sale_array );

//        $useFlg = 0;
//
//        if($sale_goods){
//            foreach($sale_goods as $sale){
//                if(in_array($sale['useFlg'], array(1, 2)) && $sale['postID'] != $save_salegoods->postID){
//                    $useFlg++;
//                }
//            }
//        }

//        if(in_array($params['useFlg'], array(1, 2))){
//            $useFlg++;
//        }

//        if($useFlg > 0){
//            $useFlg = 1;
//        }

        //$save_listgoods->useFlg = $useFlg;
        //更新二级商品useFlg      add by wanglulu 20141203   end

		$this->dbca->beginTransaction();
		try {
			//商品映射规格
			if (isset($save_spec_relation)) {
				$this->dbca->save($save_spec_relation);
			}
			
			//二级商品
			$this->dbca->save($save_listgoods);
			
			//三级商品
			$this->dbca->save($save_salegoods);
			
			//配件
//			$this->dbca->stmt_query('DELETE FROM goods_fittings_relation WHERE postID = ?', 'i', $params['tPostID']);
//			if (!empty($fitting_array)) {
//				foreach ($fitting_array as $fit) {
//					$save_fittings_relation = new entity('goods_fittings_relation');
//					$save_fittings_relation->creator = $arg_array['creator'];
//					$save_fittings_relation->postID = $params['tPostID'];
//					$save_fittings_relation->fitID = $fit['fitID'];
//					$save_fittings_relation->fitName = $fit['fitName'];
//					$save_fittings_relation->fitUnit = $fit['fitUnit'];
//					$save_fittings_relation->quantity = $fit['fitCount'];
//					$save_fittings_relation->forsale = $fit['fitSale'];
//					$save_fittings_relation->price = $fit['fitPrice'];
//					$this->dbca->save($save_fittings_relation);
//				}
//			}

//            $this->dbca->stmt_query('DELETE FROM goods_deliver_relation WHERE postID = ?', 'i', $params['tPostID']);
//            if (isset($params['interval']) && $params['interval'] == 0) {
//                if (!empty($dTime_array)) {
//                    foreach ($dTime_array as $dT) {
//                        $save_deliver_relation = new entity('goods_deliver_relation');
//                        $save_deliver_relation->creator = $arg_array['creator'];
//                        $save_deliver_relation->postID = $params['tPostID'];
//                        $save_deliver_relation->startOrderTime = $dT[0];
//                        $save_deliver_relation->endOrderTime = $dT[1];
//                        $save_deliver_relation->startDeliverTime = $dT[2];
//                        $save_deliver_relation->endDeliverTime = $dT[3];
//                        $this->dbca->save($save_deliver_relation);
//                    }
//                }
//            }
			
			$this->dbca->stmt_query('DELETE FROM goods_deliver_relation WHERE postID = ?', 'i', $params['tPostID']);
			if (isset($params['interval']) && $params['interval'] == 0) {
				if (!empty($dTime_array)) {
					foreach ($dTime_array as $dT) {
						$save_deliver_relation = new entity('goods_deliver_relation');
						$save_deliver_relation->creator = $arg_array['creator'];
						$save_deliver_relation->postID = $params['tPostID'];
						$save_deliver_relation->startOrderTime = $dT[0];
						$save_deliver_relation->endOrderTime = $dT[1];
						$save_deliver_relation->startDeliverTime = $dT[2];
						$save_deliver_relation->endDeliverTime = $dT[3];
						$this->dbca->save($save_deliver_relation);
					}
				}
			}
			
			$this->dbca->commit();
		} catch (Exception $e) {
			error_log($e->getMessage());
			$this->dbca->rollBack();
			return array('code' => 404, 'message' => '商品SKU保存失败');
		}
	
//		$useFlgStr = '';
//		if ($params['useFlg'] == 0) {
//			$useFlgStr = '下架';
//		} else if ($params['useFlg'] == 1) {
//			$useFlgStr = '上架';
//		} else if ($params['useFlg'] == 2) {
//			$useFlgStr = '售罄';
//		}
		$call_array = array(
			'product_code' => $params['product_code'],
			'salePrice' => $params['salePrice'],
			//'aheadTime' => $params['aheadTimeText'],
			//'useFlg' => $useFlgStr,
			'forsale' => $params['forsale'] == 1 ? '可销售' : '不可销售',
			//'foretaste' => $params['foretaste'] == 1 ? '可试吃' : '不可试吃',
			'specGroupID' => $params['hSpecGroupID'],
			'specID' => $params['hSpecID'],
			'tPostID' => $params['tPostID']
		);

    	return array('code' => 200, 'message' => '商品SKU保存成功', 'data' => $call_array);
    }
    
    /**
     * 查询当前三级商品并展示在页面
     *
     * @param unknown_type $arg_array
     */
    public function searchSaleShow( $arg_array ){
    	
    	//检验参数
    	if (!isset($arg_array['postID']) || !is_numeric($arg_array['postID'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}
    	
    	//数据库查询
    	quanli_include('module/base/goods/quanliGoods.class.php');
    	$ebsigHandler = new quanliGoods( $this->dbca );
    	$sale_array = array(
    		'postID' => $arg_array['postID']
    	);
    	$sale_result = $ebsigHandler->searchSaleEdit( $sale_array );
    	if (!$sale_result) {
    		return array('code' => 400, 'message' => '商品信息未找到');
    	}
    	$sale_result = $sale_result[0];
    	
    	//已选切分
    	$sync_total = array();
    	if (!empty($sale_result['syncID']) && !empty($sale_result['syncopate'])) {
    		$syncID_array = explode('|', $sale_result['syncID']);
    		$syncopate_array = explode('|', $sale_result['syncopate']);
    		if ($syncID_array && $syncopate_array) {
    			foreach ($syncID_array as $key=>$s) {
    				$sync_total[] = array(
    					'id' => $s,
    					'name' => $syncopate_array[$key]
    				);
    			}
    		}
    	}
    	$sale_result['sync_total'] = $sync_total;
    	
    	//已选生日牌
    	$birth_total = array();
    	if (!empty($sale_result['birthID']) && !empty($sale_result['birthcard'])) {
    		$birthID_array = explode('|', $sale_result['birthID']);
    		$birthcard_array = explode('|', $sale_result['birthcard']);
    		if ($birthID_array && $birthcard_array) {
    			foreach ($birthID_array as $key=>$b) {
    				$birth_total[] = array(
    					'id' => $b,
    					'name' => $birthcard_array[$key]
    				);
    			}
    		}
    	}
    	$sale_result['birth_total'] = $birth_total;
    	
    	//判断是否为主商品
    	$list_array = array(
    		'postID' => $sale_result['twoPostID']
    	);
    	$list_result = $ebsigHandler->searchListEdit( $list_array );
    	if (!$list_result) {
    		return array('code' => 400, 'message' => '商品信息未找到');
    	}
    	$list_result = $list_result[0];
    	
    	if ($sale_result['postID'] == $list_result['uid']) {
    		$sale_result['mainGoodsFlg'] = 1;
    	} else {
    		$sale_result['mainGoodsFlg'] = 0;
    	}
    	
    	//网站状态、销售状态、试吃状态
    	if ($sale_result['useFlg'] == 0) {
    		$sale_result['useFlg'] = 2;
    	} else if ($sale_result['useFlg'] == 2) {
    		$sale_result['useFlg'] = 3;
    	}
    	
    	if ($sale_result['forsale'] == 0) {
    		$sale_result['forsale'] =  2;
    	}
    	
    	if ($sale_result['foretaste'] == 0) {
    		$sale_result['foretaste'] =  2;
    	}
    	
    	//配件
    	quanli_include('module/base/goods/goodsFittings.class.php');
    	$fitHandler = new goodsFittings( $this->dbca );
    	$fit_array = array(
    		'postID' => $arg_array['postID']
    	);
    	$fit_result = $fitHandler->searchGoodsFitsRelation( $fit_array );
    	if (!$fit_result) {
    		$sale_result['fit'] = '';
    	} else {
    		$sale_result['fit'] = $fit_result;
    	}
    	
    	//配送间隔时间
    	$space_array = array(
    		'postID' => $arg_array['postID'],
    		'sortname' => 'startDeliverTime',
    		'sortorder' => 'DESC'
    	);
    	$space_result = $this->searchGoodsSpace( $space_array );
		if (!$space_result) {
    		$sale_result['space'] = '';
    	} else {
    		$sale_result['space'] = $space_result;
    	}

    	return array('code' => 200, 'message' => '查询成功', 'data' => $sale_result);
    }

    /**
     * 查询配送间隔时间
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchGoodsSpace( &$arg_array ) {
    
    	//sql字符串
    	$sql = '';
    
    	//参数类型字符串
    	$params_type = '';
    
    	//参数值数组
    	$params_array = array();
    
    	//网购号
    	if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
    		$sql .= ' AND postID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['postID'];
    	}
    
    	//扩展
    	if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
    		$sql .= $arg_array['extend'];
    	}
    
    	if ($sql) {
    		$sql = 'WHERE ' . substr($sql, 4);
    	}
    
    	try {
    
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    
    			//统计数量
    			//$search_sql = 'SELECT COUNT(*) AS item_count FROM goods_deliver_relation ' . $sql;
    			$search_sql = 'SELECT COUNT(*) AS item_count FROM goods_deliver_relation_edit ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count = $count_array[0]->item_count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    
    			$arg_array['count'] = $item_count;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    
    		} else {
    
    			$arg_array['count'] = 0;
    
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
    
    		}
    
    		//查询分类信息
    		//$search_sql = 'SELECT * FROM goods_deliver_relation ' . $sql;


    		$search_sql = 'SELECT * FROM goods_deliver_relation_edit ' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    		if (!$temp_item_array){
                $search_sql = 'SELECT * FROM goods_deliver_relation ' . $sql;
                $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            }

            if (!$temp_item_array)
                return null;

    		foreach($temp_item_array as &$item) {
    
    			$item_array[] = array(
					'postID' => $item->postID,
					'startOrderTime' => $item->startOrderTime,
					'endOrderTime' => $item->endOrderTime,
					'startDeliverTime' => $item->startDeliverTime,
					'endDeliverTime' => $item->endDeliverTime
    			);
    
    		}
    
    		return $item_array;
    
    	} catch (Exception $e) {
    
    		error_log($e->getMessage());
    		return null;
    
    	}
    
    }
    
    /**
     * 保存sku成本信息
     *
     * @param unknown_type $arg_array
     */
    public function saveCost( &$arg_array ){
    	
    	//检验参数
    	if (!isset($arg_array['postID'], $arg_array['flg'], $arg_array['cost'])) {
    		return array('code' => 400, 'message' => '缺少参数');
    	}
    	
    	if (!in_array($arg_array['flg'], array(1, 2, 3))) {
    		return array('code' => 400, 'message' => '参数错误，请刷新页面后重新操作');
    	}
    	
    	if (!is_money($arg_array['cost'], 1)) {
    		return array('code' => 400, 'message' => '请输入正确的商品成本');
    	}
    	
    	$check_listgoods = new entity('goods_sale_edit');
    	$check_listgoods->postID = $arg_array['postID'];
    	if (!$this->dbca->fetch($check_listgoods)) {
    		return array('code' => 404, 'message' => '商品信息未找到');
    	}
    	
    	//保存
    	$save_qs_relation = new entity('goods_qs_relation');
    	$save_qs_relation->postID = $arg_array['postID'];
    	if ($this->dbca->fetch($save_qs_relation)) {
    		$save_qs_relation->editor = $arg_array['creator'];
    	} else {
    		$save_qs_relation->creator = $arg_array['creator'];
    	}
    	if ($arg_array['flg'] == 1) {
    		$save_qs_relation->noTaxCost = $arg_array['cost'];
    	} else if ($arg_array['flg'] == 2) {
    		$save_qs_relation->taxCost = $arg_array['cost'];
    	} else {
    		$save_qs_relation->checkPrice = $arg_array['cost'];
    	}
    	$this->dbca->save($save_qs_relation);
    	
    	$data = array(
    		'flg' => $arg_array['flg'],
    		'postID' => $arg_array['postID'],
    		'type' => 1,
    		'cost' => $arg_array['cost']
    	);
    	return array('code' => 200, 'message' => '保存成功', 'data' => $data);
    }
    
    /**
     * 保存生产信息
     *
     * @param unknown_type $arg_array
     */
    public function saveProduct( &$arg_array ) {
    	
    	//检验参数
    	if (!isset($arg_array['params']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '缺少参数');
    	}

    	$error_msg = '';
    	$product_array = array();
    	$nutri_array = array();
    	$params = $arg_array['params'];
    	
    	//检验基本参数
    	if (!isset($params['recipe']) || empty($params['recipe'])) {
    		$error_msg .= '请填写配料信息<br/>';
    	}
    	
    	if (!isset($params['netWeight']) || empty($params['netWeight'])) {
    		$error_msg .= '请填写净含量信息<br/>';
    	}
    	
    	if (!isset($params['qsName']) || empty($params['qsName'])) {
    		$error_msg .= '请填写QS生产单品名<br/>';
    	}
    	
    	if (!isset($params['selectPgroupID']) || empty($params['selectPgroupID'])) {
    		$error_msg .= '请选择生产信息维度<br/>';
    	}
    	
    	if (!isset($params['selectNgroupID']) || empty($params['selectNgroupID'])) {
    		$error_msg .= '请选择营养成分信息<br/>';
    	}
    	
    	if (!empty($error_msg)) {
    		return array('code' => 400, 'message' => $error_msg);
    	}
    	
    	//生产信息
    	quanli_include('module/base/goods/goodsProduce.class.php');
    	$produceHandler = new goodsProduce( $this->dbca );
    	foreach ($params['selectPgroupID'] as $p) {
    		$ary_array = array(
	    		'pGroupID' => $p,
				'useFlg' => 1
			);
        	$product = $produceHandler->goodsProduceGroup( $ary_array );
        	if (!$product) {
        		return array('code' => 404, 'message' => '生产信息维度不存在或已禁用');
        	}
        	$product = $product[0];
        	
        	if (!isset($params['pGroupID_' . $p]) || $params['pGroupID_' . $p] == 0) {
        		return array('code' => 404, 'message' => '请选择生产信息维度值');
        	}
        	
        	$ary_array = array(
	    		'pid' => $params['pGroupID_' . $p],
				'useFlg' => 1
			);
			$product_value = $produceHandler->goodsProduces( $ary_array );
			if (!$product_value) {
				return array('code' => 404, 'message' => '生产信息维度值不存在或已禁用');
			}
			$product_value = $product_value[0];
			$product_array[] = array(
				'postID' => $params['postID'],
				'pGroupID' => $p,
				'pGroupName' => $product['pGroupName'],
				'pid' => $params['pGroupID_' . $p],
				'pName' => $product_value['pName']
			);
    	}
    	
    	//营养成分信息
    	quanli_include('module/base/goods/goodsNutrition.class.php');
    	$nutrHandler = new goodsNutrition( $this->dbca );
    	foreach ($params['selectNgroupID'] as $n) {
    		$ary_array = array(
	    		'nGroupID' => $n,
				'useFlg' => 1
			);
        	$nutrition = $nutrHandler->searchgoodsNutrition( $ary_array );
        	if (!$nutrition) {
        		return array('code' => 404, 'message' => '营养成分信息不存在或已禁用');
        	}
        	$nutrition = $nutrition[0];
        	if (!isset($params['nGroupID_' . $n]) || $params['nGroupID_' . $n] == 0) {
        		return array('code' => 404, 'message' => '请选择营养成分信息值');
        	}
        	
        	$ary_array = array(
	    		'nid' => $params['nGroupID_' . $n],
				'useFlg' => 1
			);
			$nutrition_value = $nutrHandler->searchNutrition( $ary_array );
			$nutrition_value = $nutrition_value[0];
			if (!$nutrition_value) {
				return array('code' => 404, 'message' => '营养成分信息值不存在或已禁用');
			}
			
			$nutri_array[] = array(
				'postID' => $params['postID'],
				'nGroupID' => $n,
				'nGroupName' => $nutrition['nGroupName'],
				'nid' => $params['nGroupID_' . $n],
				'name' => $nutrition_value['name'],
				'npct' => $nutrition_value['npct']
			);
    	}
    	
    	//检查数据库
    	$check_salegoods = new entity('goods_sale_edit');
    	$check_salegoods->postID = $params['postID'];
    	if (!$this->dbca->fetch($check_salegoods)) {
    		return array('code' => 400, 'message' => '商品信息未找到');
    	}
    	
		//保存数据
		$save_goods_qs_relation = new entity('goods_qs_relation');
		$save_goods_qs_relation->postID = $params['postID'];
		if ($this->dbca->fetch($save_goods_qs_relation)) {
			$save_goods_qs_relation->editor = $arg_array['creator'];
		} else {
			$save_goods_qs_relation->creator = $arg_array['creator'];
		}
		$save_goods_qs_relation->qsName = $params['qsName'];
		$save_goods_qs_relation->recipe = $params['recipe'];
		$save_goods_qs_relation->weight = $params['netWeight'];
		
		$this->dbca->beginTransaction();
		try {
			$this->dbca->save($save_goods_qs_relation);
			
			//生产信息映射
			$this->dbca->stmt_query('DELETE FROM goods_produce_relation WHERE postID = ?', 'i', $params['postID']);
			if (!empty($product_array))	{
				foreach ($product_array as $p_l) {
					$save_goods_produce_relation = new entity('goods_produce_relation');
					$save_goods_produce_relation->creator = $arg_array['creator'];
					$save_goods_produce_relation->postID = $p_l['postID'];
					$save_goods_produce_relation->pGroupID = $p_l['pGroupID'];
					$save_goods_produce_relation->pGroupName = $p_l['pGroupName'];
					$save_goods_produce_relation->pid = $p_l['pid'];
					$save_goods_produce_relation->pName = $p_l['pName'];
					$this->dbca->save($save_goods_produce_relation);
				}
			}
			
			//营养成分信息映射
			$this->dbca->stmt_query('DELETE FROM goods_nutrition_relation WHERE postID = ?', 'i', $params['postID']);
			if (!empty($nutri_array)) {
				foreach ($nutri_array as $n_l) {
					$save_goods_nutrition_group = new entity('goods_nutrition_relation');
					$save_goods_nutrition_group->creator = $arg_array['creator'];
					$save_goods_nutrition_group->postID = $n_l['postID'];
					$save_goods_nutrition_group->nGroupID = $n_l['nGroupID'];
					$save_goods_nutrition_group->nGroupName = $n_l['nGroupName'];
					$save_goods_nutrition_group->nid = $n_l['nid'];
					$save_goods_nutrition_group->name = $n_l['name'];
					$save_goods_nutrition_group->npct = $n_l['npct'];
					$this->dbca->save($save_goods_nutrition_group);
				}
			}
			
			$this->dbca->commit();
			
			$data = array(
	    		'type' => 2,
	    		'recipe' => $params['recipe'],
	    		'weight' => $params['netWeight'],
	    		'qsName' => $params['qsName']
	    	);
	    	
    		return array('code' => 200, 'message' => '生产信息保存成功', 'data' => $data);
    		
		} catch (Exception $e) {
			error_log($e->getMessage());
			$this->dbca->rollBack();
			return array('code' => 400, 'message' => '生产信息保存失败');
		}
    }
    
    /**
     * 查询已选‘生产信息’和‘营养成分’
     *
     * @param unknown_type $arg_array
     */
    public function searchSelProduct( &$arg_array ){
    	
    	//检验参数
    	if (!isset($arg_array['postID']) || !is_numeric($arg_array['postID'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}
    	
    	$check_salegoods = new entity('goods_sale_edit');
    	$check_salegoods->postID = $arg_array['postID'];
    	if (!$this->dbca->fetch($check_salegoods)) {
			return array('code' => 404, 'message' => '商品信息不存在');
    	}
    	
    	$qs_array = array(
    		'postID' => $arg_array['postID']
    	);
    	
    	$nu_array = array(
    		'postID' => $arg_array['postID']
    	);
    	
    	//生产信息
    	quanli_include('module/base/goods/goodsProduce.class.php');
    	$productHandler = new goodsProduce( $this->dbca );
        $product_relation = $productHandler->searchGoodsProducesRelation( $qs_array );
        $product_array['params'] = array();
        $product_sel = array();
        $product_result = '';
        if ($product_relation) {
        	foreach ($product_relation as $p) {
        		$product_array['params'][] = $p['pGroupID'];
        		$product_sel[] = array(
        			'pGroupID' => $p['pGroupID'],
        			'pid' => $p['pid']
        		);
        	}
        	$product_result = $productHandler->searchProduct( $product_array ) ;
        	if ($product_result['code'] != 200) {
        		return $product_result;
        	}
        }

    	//营养信息
		quanli_include('module/base/goods/goodsNutrition.class.php');
		$nutrHandler = new goodsNutrition( $this->dbca );
        $nutrition_relation = $nutrHandler->searchGoodsNutriRelation( $nu_array );
        $nutrition_array['params'] = array();
        $nutrition_sel = array();
        $nutrition_result = '';
		if ($nutrition_relation) {
        	foreach ($nutrition_relation as $n) {
        		$nutrition_array['params'][] = $n['nGroupID'];
        		$nutrition_sel[] = array(
        			'nGroupID' => $n['nGroupID'],
        			'nid' => $n['nid'],
        			'npct' => $n['npct']
        		);
        	}
        	$nutrition_result = $nutrHandler->searchNutri( $nutrition_array ) ;
        	if ($nutrition_result['code'] != 200) {
        		return $nutrition_result;
        	}
        }
        
        //qs信息
        quanli_include('module/base/goods/quanliGoods.class.php');
        $goodsHandler = new quanliGoods( $this->dbca );
        $goodsqs_array = $goodsHandler->searchGoodsQs( $qs_array );
        if ($goodsqs_array) {
        	$goodsqs_array = $goodsqs_array[0];
        }
        
        $data = array(
        	'product_result' => isset($product_result['data']) ? $product_result['data'] : '',
        	'product_sel' => $product_sel,
        	'nutrition_result' => isset($nutrition_result['data']) ? $nutrition_result['data'] : '',
        	'nutrition_sel' => $nutrition_sel,
        	'recipe' => isset($goodsqs_array['recipe']) ? $goodsqs_array['recipe'] : '',
        	'weight' => isset($goodsqs_array['recipe']) ? $goodsqs_array['weight'] : '',
        	'qsName' => isset($goodsqs_array['qsName']) ? $goodsqs_array['qsName'] : '',
        	'type' => 3
        );
        return array('code' => 200, 'message' => '查询成功', 'data' => $data);
    }
    
    /**
     * 保存商品成本信息
     *
     * @param unknown_type $arg_array
     */
    public function saveCostInfo( &$arg_array ){
    	//检验参数
    	if (!isset($arg_array['params']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误，请刷新页面后重新操作');
    	}
    	
    	$error_msg = '';
    	$params = $arg_array['params'];
    	
    	if (!isset($params['postID']) || !quanli_is_int($params['postID'])) {
    		return array('code' => 400, 'message' => '参数错误，请刷新页面后重新操作');
    	}
    	
    	if (!isset($params['noTaxCost']) || !is_money($params['noTaxCost'], 2)) {
    		$error_msg .= '请填写正确的不含税成本<br/>';
    	}
    	
    	if (!isset($params['taxCost']) || !is_money($params['taxCost'], 2)) {
    		$error_msg .= '请填写正确的含税成本<br/>';
    	}
    	
    	if (!isset($params['checkPrice']) || !is_money($params['checkPrice'], 2)) {
    		$error_msg .= '请填写正确的销售结算价<br/>';
    	}
    	
    	//保存
    	$save_cost = new entity('goods_qs_relation');
    	$save_cost->postID = $params['postID'];
    	if ($this->dbca->fetch($save_cost)) {
    		$save_cost->editor = $arg_array['creator'];
    	} else {
    		$save_cost->creator = $arg_array['creator'];
    	}
    	$save_cost->noTaxCost = $params['noTaxCost'];
    	$save_cost->taxCost = $params['taxCost'];
    	$save_cost->checkPrice = $params['checkPrice'];
    	$this->dbca->save($save_cost);
    	
    	$data = array(
    		'type' => 1,
    		'noTaxCost' => round($params['noTaxCost'], 2),
    		'taxCost' => round($params['taxCost'], 2),
    		'checkPrice' => round($params['checkPrice'], 2)
    	);
    	
    	return array('code' => 200, 'message' => '保存成功', 'data' => $data);
    }
    
    /**
     * 保存商品图片信息
     *
     * @param unknown_type $arg_array
     */
    public function savePicInfo( &$arg_array ){
    	
    	//检验参数
    	if (!isset($arg_array['params']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误，请刷新页面后重新操作');
    	}
    	
    	//设置参数
    	$img_array = array();
    	$params = $arg_array['params'];
    	
    	//检查商品网购号
    	if (!isset($params['postID']) || !quanli_is_int($params['postID'])) {
    		return array('code' => 400, 'message' => '参数错误，请刷新页面后重新操作');
    	}
    	
    	if (!in_array($params['cover'], array(1, 2, 3, 4, 5))) {
        	return array('code' => 404, 'message' => '请选择封面设置');
        }

    	//二级商品
        $check_list_edit = new entity('goods_list_edit');
        $check_list_edit->postID = $params['postID'];
        if (!$this->dbca->fetch($check_list_edit)) {
        	return array('code' => 404, 'message' => '商品信息未找到');
        }

        //检查所有图片
		for ($i=1; $i<=5; $i++) {
			$big_img = $params['imgname_big_' . $i];
			$middle_img = $params['imgname_middle_' . $i];
			$list_img = $params['imgname_list_' . $i];
			$small_img = $params['imgname_small_' . $i];
			$iSort = $params['iSort_' . $i];
			$mainFlg = 0;
			if (empty($big_img) && empty($middle_img) && empty($list_img) && empty($small_img)) {
				continue;
			} else {
				if (empty($big_img)) {
					return array('code' => 400, 'message' => '商品图片' . $i . '：960 x 960大小的图片未上传');
				}
				if (empty($middle_img)) {
					return array('code' => 400, 'message' => '商品图片' . $i . '：480 x 480大小的图片未上传');
				}
				if (empty($list_img)) {
					return array('code' => 400, 'message' => '商品图片' . $i . '：216 x 216大小的图片未上传');
				}
				if (empty($small_img)) {
					return array('code' => 400, 'message' => '商品图片' . $i . '：100 x 100大小的图片未上传');
				}
				if (!quanli_is_int($iSort)) {
					return array('code' => 400, 'message' => '商品图片' . $i . '：请填写正确的排序序号');
				}
				if ($i == $params['cover']) {
					$mainFlg = 1;
				}
				$img_array[] = array(
					'big_img' => $big_img,
					'middle_img' => $middle_img,
					'list_img' => $list_img,
					'small_img' => $small_img,
					'sort' => $iSort,
					'mainFlg' => $mainFlg
				);
			}
		}
    	
    	if (empty($img_array)) {
    		return array('code' => 400, 'message' => '请上传商品图片');
    	}

    	$pathExp = date('Ymd');
    	
    	//应该移动到的目录
		$saveDir = G_APPLICATION_BASEPATH.'quanliadmin/docroot/images/goods/' . $pathExp . '/' . $check_list_edit->goodsID . '/';
		if (!@file_exists($saveDir)) {
			mkdir($saveDir, 0777, true);
		}
    	
		try {
			$this->dbca->beginTransaction();
			
			//删除旧的商品图片信息
    		$this->dbca->stmt_query('DELETE FROM goods_picture WHERE postID = ?', 'i', $params['postID']);
    		
	    	foreach ($img_array as $img) {
	    		
	    		$big_extend = strtolower(substr(strrchr($img['big_img'] , '.'), 1));
	    		$middle_extend = strtolower(substr(strrchr($img['middle_img'] , '.'), 1));
	    		$list_extend = strtolower(substr(strrchr($img['list_img'] , '.'), 1));
	    		$small_extend = strtolower(substr(strrchr($img['small_img'] , '.'), 1));
	    		
	    		//上传后的临时存放路径
				$big_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/kindeditor/' . $img['big_img'];
				$middle_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/kindeditor/' . $img['middle_img'];
				$list_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/kindeditor/' . $img['list_img'];
				$small_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/kindeditor/' . $img['small_img'];
				
				if (!@file_exists($big_path)) {
					$big_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/goods/' . $img['big_img'];
				}
				if (!@file_exists($middle_path)) {
					$middle_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/goods/' . $img['middle_img'];
				}
				if (!@file_exists($list_path)) {
					$list_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/goods/' . $img['list_img'];
				}
				if (!@file_exists($small_path)) {
					$small_path = G_APPLICATION_BASEPATH . 'quanliadmin/docroot/images/goods/' . $img['small_img'];
				}
				
				$big_pic = 'big_' . $check_list_edit->goodsID . '_'. $img['sort'] . '.' . $big_extend;
				$middle_pic = 'middle_' . $check_list_edit->goodsID . '_'. $img['sort'] . '.' . $middle_extend;
				$list_pic = 'list_' . $check_list_edit->goodsID . '_'. $img['sort'] . '.' . $list_extend;
				$small_pic = 'small_' . $check_list_edit->goodsID . '_'. $img['sort'] . '.' . $small_extend;
	    		
	    		if (!@file_exists($saveDir . $big_pic) || $big_path != $saveDir . $big_pic) {
	    			if (@file_exists($saveDir . $big_pic)) {
	    				@unlink($saveDir . $big_pic);
	    			}
	    			
					copy($big_path, $saveDir . $big_pic);
	    		}
	    		if (!@file_exists($saveDir . $middle_pic) || $middle_path != $saveDir . $middle_pic) {
	    			if (@file_exists($saveDir . $middle_pic)) {
	    				@unlink($saveDir . $middle_pic);
	    			}
	    			
					copy($middle_path, $saveDir . $middle_pic);
	    		}
	    		if (!@file_exists($saveDir . $list_pic) || $list_path != $saveDir . $list_pic) {
	    			if (@file_exists($saveDir . $list_pic)) {
	    				@unlink($saveDir . $list_pic);
	    			}
	    			
					copy($list_path, $saveDir . $list_pic);
	    		}
	    		if (!@file_exists($saveDir . $small_pic) || $small_path != $saveDir . $small_pic) {
	    			if (@file_exists($saveDir . $small_pic)) {
	    				@unlink($saveDir . $small_pic);
	    			}
	    			
					copy($small_path, $saveDir . $small_pic);
	    		}
				if ($img['mainFlg'] == 1) {
					$check_list_edit->goodsMainPic = $pathExp . '/' . $check_list_edit->goodsID . '/' . $list_pic;
				}
				
				$save_goods_picture = new entity('goods_picture');
				$save_goods_picture->creator = $arg_array['creator'];
				$save_goods_picture->postID = $params['postID'];
				$save_goods_picture->pictureLink = $pathExp . '/' . $check_list_edit->goodsID  . '/' . $list_pic;
				$save_goods_picture->sortOrder = $img['sort'];
				$save_goods_picture->mainFlg = $img['mainFlg'];
				$this->dbca->save($save_goods_picture);
				
	    	}
	    	
	    	$this->dbca->save($check_list_edit);
	    	$this->dbca->stmt_query('UPDATE goods_sale_edit SET goodsMainPic = ? WHERE twoPostID = ?', 'si', $check_list_edit->goodsMainPic, $params['postID']);
	    	$this->dbca->stmt_query('UPDATE goods_sale SET goodsMainPic = ? WHERE twoPostID = ?', 'si', $check_list_edit->goodsMainPic, $params['postID']);
	    	$this->dbca->stmt_query('UPDATE goods_list SET goodsMainPic = ? WHERE postID = ?', 'si', $check_list_edit->goodsMainPic, $params['postID']);
	    	$this->dbca->commit();
	    	return array('code' => 200, 'message' => '图片保存成功');
		} catch (Exception $e){
			$this->dbca->rollBack();
			return array('code' => 200, 'message' => '图片保存失败');
		}
    }
}